package com.house.house.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.house.house.entity.House;




public class HouseQueryDto {
	
	private String houseNo;
	private String houseType;
	
	
	public String getHouseNo() {
		return houseNo;
	}


	public void setHouseNo(String houseNo) {
		this.houseNo = houseNo;
	}


	public String getHouseType() {
		return houseType;
	}


	public void setHouseType(String houseType) {
		this.houseType = houseType;
	}


	public static Specification<House> getSpecification(HouseQueryDto houseQueryDto){
		Specification<House> spec = new Specification<House>() {
			public Predicate toPredicate(Root<House> root,CriteriaQuery<?> query,CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>(); 
				
				 if(null != houseQueryDto && !StringUtils.isEmpty(houseQueryDto.getHouseNo())) {
					 list.add(cb.like(root.get("houseNo").as(String.class), "%"+houseQueryDto.getHouseNo()+"%"));
				 }
				 if(null != houseQueryDto && !StringUtils.isEmpty(houseQueryDto.getHouseType())) {
					 list.add(cb.like(root.get("houseType").as(String.class), "%"+houseQueryDto.getHouseType()+"%"));
				 }
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		};
		return spec;
		
	}
}
